	<?php

function is_user_online($fbid, $format = "general")
{
		//$db = new database('localhost', 'roopunkc_gausipp', 'thedynamicme', 'roopunkc_gausipp');	
		$arr = explode("_", $format);
		$format = $arr[0];

		if($format == "reduced")
			{
				$online_text = '<div class="circle online popup-text" title="online"></div>';
				$offline_text = '<div class="circle offline popup-text" title="offline"></div>';
				$nouser_text = '<div class="circle nouser popup-text" title="not on gausipp yet"></div>';
			}
		else if($format == "general")
			{
				$online_text = '<span class="online online_status">online</span>';
			$offline_text = '<span class="offline online_status">offline</span>';
			$nouser_text = '<span class="offline online_status">offline</span>';
			}
		else if($format == "invite")
		{
			$online_text = "";
			$offline_text = "";
			$nouser_text = ' - <span class="a small-font" title="invite" onclick=\'sendRequestToRecipients("'.$fbid.'", "'.$arr[1].'")\'>invite</span>';
		}
		else if($format == "boolean")
		{
			$online_text = 1;
			$offline_text = 0;
			$nouser_text = 0;
		}
			
		$login = mysql_query("SELECT * FROM site_user_login WHERE `fbid`='$fbid' ORDER BY `time_out` DESC LIMIT 1");
		if(mysql_num_rows(mysql_query("SELECT * FROM site_user_meta WHERE `fbid`='$fbid'")) == 0)
			return $nouser_text; 
		
		$time_out = mysql_result($login,0,'time_out');
		$time_present = time();
	
		if(($time_present - $time_out) < 35)
		return $online_text;
		else return $offline_text; 
} 
  

// this function requires, session_start() to be decalred, before this function is called
function register_user()
{
	//$error .= "in";	
	$user = $_SESSION['user'];
	$session_id = session_id();
	$time = time();
	
	$temp = mysql_query("SELECT * FROM site_user_login WHERE `fbid`='$user' AND `session_id`='$session_id' ORDER BY `id` DESC LIMIT 1");
	if(!$temp)
		 $error .= mysql_numrows($temp);
	
	if(mysql_num_rows($temp) == 0) return;	
	
	//echo '<script>alert('.mysql_numrows($temp).');</script>';
	$last_id = mysql_result($temp, 0, 'id');
		
	$result = mysql_query("UPDATE site_user_login SET `time_out`='$time' WHERE `id`='$last_id'");
	//	if(!$result)
		// $error .= mysql_affected_rows();
	
	
	
}

function user_unread($fbid)
{
	$arr1 = explode("_",$fbid);
	$id = $arr1[0];
	// counter
	$count = 0;
	$output = array();
	
		$temp = mysql_query("SELECT * FROM site_pms WHERE `type` LIKE '%$id%' AND `del`!='1' AND `read`!='11'");
	if(!$temp)  echo '<script>alert("'.mysql_error().'");</script>';
	
	
	
		while($row = mysql_fetch_array($temp))
		{
					 
		$name = $row['name'];
		$type = $row['type'];
		$fbid1 = $row['fbid'];
		 
		$arr = explode("_",$type);
		if($arr[1] == $id)
			$pattern = "01";
		else $pattern = "10";
		
		if($row['read'] == $pattern) {// this is an unread message
		// for the detail part
		if($arr1[1] == "detail") {
			if(array_key_exists($name."_".$fbid1, $output)) $output[$name."_".$fbid1]++;
			else $output[$name."_".$fbid1]=1;
		}
		// for the other part
		 $count++;
		}
		}
	
		//return $arr[1];
		if($arr1[1] == "detail") {
			
			
			$keys = array_keys($output);
			//print_r($keys);
			if(sizeof($keys)!=0) $str= '<strong>Unread</strong><br><br>'; 
			foreach($keys as $key)
			{
				
				$arr2 = explode("_",$key);
				$name_s = $arr2[0];
				$id_s  = $arr2[1];
				$str = $str.' <a href="index.php?cat=msg&fbid='.$id_s.'">'.$name_s.' ('.$output[$key].')</a> <br>';
			}		
			return $str;
			
		}	
		else {
		if($count!=0)
		return '<span class="unread_bubble">'.$count.'</span>';
		else return "";
	 	}
	 
}

function post_time($stream_type, $id)
{
	if($stream_type == "pms")
	{
		$res = mysql_query("SELECT * FROM site_pms WHERE `id`='$id'");
		$timestamp = mysql_result($res, 0, 'timestamp');
		return process_time($timestamp);
	}
	else if($stream_type == "group")
	{
		$res = mysql_query("SELECT * FROM site_posts WHERE `id`='$id'");
		$timestamp = mysql_result($res, 0, 'timestamp');
		return process_time($timestamp);
	}	
	else if($stream_type == "comment")
	{
		$res = mysql_query("SELECT * FROM site_comments WHERE `id`='$id'");
		$timestamp = mysql_result($res, 0, 'timestamp');
		return process_time($timestamp);
	}
}

function process_time($time)
{
	$time_n = time();
	$diff = $time_n - $time;
	
	if($diff < 60) 
	 { $val = $diff; if($val == '1') $unit = "second"; else $unit = "seconds";}
	elseif($diff < 3600)
	 { $val = floor($diff/60); if($val == '1') $unit = "minute"; else $unit = "minutes";}
	elseif($diff < (24*3600)) 
	 { $val = floor($diff/60/60); if($val == '1') $unit = "hour"; else $unit = "hours";}
	elseif($diff < (100*24*3600))
	 { $val = floor($diff/60/60/24); if($val == '1') $unit = "day"; else $unit = "days";}
	else
	 { $val = ""; $unit = "long time";}
	 
	 return $val." ".$unit." ago"; 
}

// for converting urls into links
function convertUrl($text)
{
	// The Regular Expression filter
	$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

	// The Text you want to filter for urls
	//$text = "The text you want to filter goes here. http://google.com";

	// Check if there is a url in the text
	if(preg_match($reg_exUrl, $text, $url)) {

		   // make the urls hyper links
		   return preg_replace($reg_exUrl, "<a href=".$url[0]." target=\"_blank\">".$url[0]."</a> ", $text);

	} else {

       // if no urls in the text just return the text
       return $text;
	   }
}


function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

function add_emoticons($out)
{
	if($out == ":)")
	$out = str_replace(":)",'<img src="images/chat/Smile.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :)",' <img src="images/chat/Smile.png" style="vertical-align:middle"/>', $out);
	
	if($out == ";)")
	$out = str_replace(";)",'<img src="images/chat/Wink.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" ;)",' <img src="images/chat/Wink.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":P")
	$out = str_replace(":P",'<img src="images/chat/Tongue.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :P",' <img src="images/chat/Tongue.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":|")
	$out = str_replace(":|",'<img src="images/chat/Ambivalent.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :|",' <img src="images/chat/Ambivalent.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":D")
	$out = str_replace(":D",'<img src="images/chat/Laugh.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :D",' <img src="images/chat/Laugh.png" style="vertical-align:middle"/>', $out);
	
	if($out == "B)")
	$out = str_replace("B)",'<img src="images/chat/Hot.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" B)",' <img src="images/chat/Hot.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":/")
	$out = str_replace(":/",'<img src="images/chat/Undecided.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :/",' <img src="images/chat/Undecided.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":(")
	$out = str_replace(":(",'<img src="images/chat/Frown.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :(",' <img src="images/chat/Frown.png" style="vertical-align:middle"/>', $out);
	
	if($out == ":*")
	$out = str_replace(":*",'<img src="images/chat/Kiss.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" :*",' <img src="images/chat/Kiss.png" style="vertical-align:middle"/>', $out);
	
	if($out == ";(")
	$out = str_replace(";(",'<img src="images/chat/Crying.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" ;(",' <img src="images/chat/Crying.png" style="vertical-align:middle"/>', $out);
	
	if($out == "O:-)")
	$out = str_replace("O:-)",'<img src="images/chat/Angel.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" O:-)",' <img src="images/chat/Angel.png" style="vertical-align:middle"/>', $out);
	
	if($out == "o_O")
	$out = str_replace("o_O",'<img src="images/chat/Confused.png" style="vertical-align:middle"/>', $out);
	else
	$out = str_replace(" o_O",' <img src="images/chat/Confused.png" style="vertical-align:middle"/>', $out);
	
	//$out = str_replace("<3",'<img src="images/chat/Heart.png" style="vertical-align:middle"/>', $out);
	
	
	
	return $out;
}

function addTags($out)
{
	$out = str_replace("[br]",'<br/>', $out);
	$out = str_replace("[b]",'<strong>', $out);
	$out = str_replace("[/b]",'</strong>', $out);
	return $out;
}
?>